package zxq.rollcall.course.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import springfox.documentation.spring.web.json.Json;
import zxq.rollcall.course.controller.action.IClassController;
import zxq.rollcall.course.entity.SystemClassTableEntity;
import zxq.rollcall.course.entity.dto.ResultDTO;
import zxq.rollcall.course.entity.dto.set.ClassDTO;
import zxq.rollcall.course.entity.query.ClassQuery;
import zxq.rollcall.course.entity.query.PageQuery;
import zxq.rollcall.course.service.MPSystemClassTableService;

import javax.xml.bind.util.JAXBSource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统班级表 前端控制器
 * </p>
 *
 * @author Yanghu
 * @since 2020-01-22
 */
@Controller
@RequestMapping("/course/api/class")
@Api(tags = "系统班级表操作")
@Slf4j
public class SystemClassTableAction implements IClassController {

    @Autowired
    MPSystemClassTableService systemClassTableService;


    @Override
    public ResultDTO<Boolean> save(ClassQuery classQuery) {
        SystemClassTableEntity systemClassTableEntity = new SystemClassTableEntity();
        BeanUtils.copyProperties(classQuery,systemClassTableEntity);
        boolean rst = systemClassTableService.save(systemClassTableEntity);
        if(rst) {
            return new ResultDTO<>(1,"插入成功",true);
        }

        return new ResultDTO<>(0,"插入失败",false);
    }

    @Override
    public ResultDTO<Boolean> update(Integer id, ClassQuery classQuery) {
        SystemClassTableEntity systemClassTableEntity = new SystemClassTableEntity();
        BeanUtils.copyProperties(classQuery,systemClassTableEntity);
        systemClassTableEntity.setId(id);
        systemClassTableEntity.setUpdateTime(LocalDateTime.now());
        boolean rst = systemClassTableService.updateById(systemClassTableEntity);
        if(rst) {
            return new ResultDTO<>(1,"更新成功",true);
        }
        return new ResultDTO<>(0,"更新失败",false);
    }

    @Override
    public ResultDTO<Boolean> delete(Integer id) {
        boolean rst = systemClassTableService.removeById(id);
        if(rst) {
            return new ResultDTO<>(1,"删除成功",true);
        }
        return new ResultDTO<>(0,"删除失败",false);
    }

    @Override
    public ResultDTO<List<ClassDTO>> query(PageQuery pageQuery) {
        Page<SystemClassTableEntity> page = new Page();
        page.setCurrent(pageQuery.getPage());
        page.setSize(pageQuery.getCount());
        Page<SystemClassTableEntity> pages = systemClassTableService.page(page);
        List<ClassDTO> classDTOS = pages.getRecords().stream().map(item -> {
            ClassDTO classDTO = new ClassDTO();
            BeanUtils.copyProperties(item,classDTO);
            return classDTO;
        }).collect(Collectors.toList());
        return new ResultDTO<>(0,"查询成功",classDTOS);
    }
}

